package com.explorx.sns.puma.im.core.controller;

import com.explorex.puma.common.web.HttpResult;
import com.explorx.sns.puma.im.core.chat.UserRelationService;
import com.explorx.sns.puma.im.core.chat.model.BlockUserDto;
import com.explorx.sns.puma.im.core.chat.model.FollowUserDto;
import com.explorx.sns.puma.im.core.chat.model.UserRelationModel;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/api/user/relation")
public class UserRelationController {


    @Resource
    private UserRelationService userRelationService;

    /**
     * /api/user/relation/v1/friend/query
     *
     * @param uid
     * @return
     */
    @GetMapping("/v1/friend/query")
    public HttpResult getUserFriends(@RequestParam Long uid) {
        final List<UserRelationModel> userRelationModels = userRelationService.queryFriendList(uid);
        return HttpResult.success(userRelationModels);

    }

    /**
     * /api/user/relation/v1/all/query
     *
     * @param uid
     * @return
     */
    @GetMapping("/v1/all/query")
    public HttpResult getUserAllContact(@RequestParam Long uid, @RequestParam Long pageIdx) {

        final List<UserRelationModel> userRelationModels = userRelationService.queryAllContact(uid, 0, 100);
        return HttpResult.success(userRelationModels);
    }

    /**
     * /api/user/relation/v1/friend/blocked
     *
     * @param uid
     * @return
     */
    @GetMapping("/v1/friend/blocked")
    public HttpResult getUserBlockedUser(@RequestParam Long uid) {
        final List<UserRelationModel> userRelationModels = userRelationService.queryBlockUserList(uid);
        return HttpResult.success(userRelationModels);
    }

    /**
     * /api/user/relation/v1/follow/change
     *
     * @param dto
     * @return
     */
    @PostMapping("/v1/follow/change")
    public HttpResult followUser(@RequestBody FollowUserDto dto) {

        if (Objects.isNull(dto.getFromUserId()) || Objects.isNull(dto.getTargetUserId())) {
            return HttpResult.fail(-1, "参数错误");
        }
        if (dto.isFollow()) {
            userRelationService.followUser(dto.getFromUserId(), dto.getTargetUserId());
        } else {
            userRelationService.unfollowUser(dto.getFromUserId(), dto.getTargetUserId());
        }
        return HttpResult.success();
    }

    /**
     * /api/user/relation/v1/block/change
     *
     * @param dto
     * @return
     */
    @PostMapping("/v1/block/change")
    public HttpResult blockedUser(@RequestBody BlockUserDto dto) {
        if (dto.isBlock()) {
            userRelationService.blockUser(dto.getFromUserId(), dto.getTargetUserId());
        } else {
            userRelationService.unblockUser(dto.getFromUserId(), dto.getTargetUserId());
        }

        return HttpResult.success();
    }

    /**
     * 可以查询到所有人的profile 信息，词接口有安全问题
     *
     * @param
     * @return
     */
    @GetMapping("/v1/profile/query")
    public HttpResult getUserProfile(@RequestParam Long fromUserId, @RequestParam Long toUserId) {
        try {
            Preconditions.checkArgument(Objects.nonNull(fromUserId), "from userId is null");
            Preconditions.checkArgument(Objects.nonNull(toUserId), "toUserId userId is null");
            final UserRelationModel userRelationModel = userRelationService.queryUserProfile(fromUserId, toUserId);
            return HttpResult.success(userRelationModel);
        } catch (IllegalArgumentException e) {
            return HttpResult.fail(-1, e.getMessage());
        } catch (Exception e) {
            log.error("/v1/profile/query exception fromUserId={},toUserId={}", fromUserId, toUserId, e);
        }
        return HttpResult.success();
    }

}
